package com.youngjun.common.mybatis.plugin;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.youngjun.common.exception.CommonExceptionType;
import com.youngjun.common.mybatis.page.PageParams;
import com.youngjun.common.oss.expection.CommonException;

import java.lang.reflect.Method;

/**
 * @author virgojun
 */
public class PageHelper {

    public static void startPage(PageParams pageParams) {
        try {
            Method method = PageMethod.class
                    .getDeclaredMethod("setLocalPage", Page.class);
            method.setAccessible(true);
            method.invoke(PageMethod.class,pageParams.getPage());
        } catch (Throwable e) {
            clearPage();
            throw new CommonException(CommonExceptionType.DB_PAGE_FAILED);
        }
    }

    public static void clearPage() {
        com.github.pagehelper.PageHelper.clearPage();
    }

}
